package com.laihui.activity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.laihui.MyApplication;
import com.laihui.activity.R;
import com.laihui.base.BaseActivity;
import com.laihui.entity.RealBean;
import com.laihui.entity.RetMap;
import com.laihui.model.Realname;
import com.laihui.model.RecomUrl;
import com.laihui.model.ToRealname;
import com.laihui.utils.ToastUtil;
import com.laihui.view.TopBar;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RealnameActivity extends BaseActivity {

    @BindView(R.id.topbar)//topbar
    TopBar topbar;
    @BindView(R.id.et_name)//名字输入框
    EditText etName;
    @BindView(R.id.et_card)//身份证输入框
    EditText etCard;
    @BindView(R.id.btn_submit)//审核按钮
    Button btnSubmit;
    @BindView(R.id.ll_tishi)//审核按钮
    LinearLayout ll_tishi;

    private Activity mActivity;
    private MyApplication application;
    private static final String TAG = "RealnameActivity";

    @Override
    public void init() {
        mActivity = RealnameActivity.this;
        application = (MyApplication) getApplication();

        topbar.setTitle("实名认证");
        topbar.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
       toRealname();
    }
    private void toRealname(){

        Map<String,String> map = new HashMap<>();
        map.put("member_id",application.userId);
        application.apiService.toReal(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RealBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RealBean toRealname) {
                        if (toRealname.item!=null){
                            etName.setText(toRealname.item.real_name);
                            etCard.setText(toRealname.item.id_number);
                            etName.setEnabled(false);
                            etCard.setEnabled(false);
                            btnSubmit.setEnabled(false);
                            btnSubmit.setText("确定");
                            ll_tishi.setVisibility(View.VISIBLE);
                        }else {
                            etCard.setText("");
                            etName.setText("");
                        }
                    }
                });
    }

    private void realname(){
        String name = etName.getText().toString().trim();
        String card = etCard.getText().toString().trim();
        Pattern idNumPattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])");
        boolean isCard = idNumPattern.matcher(card).matches();
        if (name.isEmpty()){
            ToastUtil.showToast(mActivity,"请输入身份证姓名");
            return;
        }
        if(card.isEmpty()){
            ToastUtil.showToast(mActivity,"请输入您的身份证号");
            return;
        }
        /*if (card.length()>18||card.length()<18){
            ToastUtil.showToast(mActivity,"请输入正确的身份证号码");
            return;
        }*/
        if (!isCard){
            ToastUtil.showToast(mActivity,"请输入正确的身份证号码");
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("member_id",application.userId);
        map.put("idnumber",card);
        map.put("real_name",name);

        application.apiService.certification(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RetMap>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(RetMap retMap) {
                        ToastUtil.showToast(RealnameActivity.this,retMap.message);
                        if(retMap.status.equals("1")){
                            finish();
                        }
                    }
                });

    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_realname;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }


    @OnClick(R.id.btn_submit)
    public void onClicked(View view) {
        switch (view.getId()){
            case R.id.btn_submit:
                realname();
                break;
        }
    }
}
